package com.campus.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.commons.entity.Annunciate;
import com.campus.app.mapper.AnnunciateMapper;
import com.campus.app.service.AnnunciatesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.commons.utils.MyPage;
import com.campus.commons.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.Null;
import java.util.List;

/**
 * <p>
 * 公告 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@Service
@Validated
public class AnnunciateServiceImpl extends ServiceImpl<AnnunciateMapper, Annunciate> implements AnnunciatesService {

    @Autowired
    private AnnunciateMapper annunciateMapper;

    @Override
    public Result<List<Annunciate>> getAllAnnunciates( MyPage myPage,boolean filter) {
        return result(myPage, new QueryWrapper(),filter);

    }

    @Override
    public Result<List<Annunciate>> getAnnunciates( MyPage<Annunciate> myPage) {
        QueryWrapper<Annunciate> queryWrapper = new QueryWrapper<>();
        Annunciate data = myPage.getData();
        queryWrapper.eq(data.getAnnTypeId() != null, "annTypeId",data.getAnnTypeId());
        queryWrapper.eq(data.getState() != null, "state",data.getState());
        return Result.result(annunciateMapper.selectPage(new Page<>(myPage.getPage(),myPage.getLimit()),queryWrapper));
    }


    @Override
    public Result upAnnunciate(@Valid Annunciate annunciate) {
        return Result.result(annunciateMapper.updateById(annunciate));
    }

    @Override
    public Result addAnnunciate( Annunciate annunciate) {
        return Result.result(annunciateMapper.insert(annunciate));
    }

    @Override
    public Result deleteAnnunciateById(Integer id) {
        return Result.result(annunciateMapper.deleteById(id));
    }

    @Override
    public Result deleteAnnunciateByType(Integer types) {
        QueryWrapper<Annunciate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("annTypeId", types);
        return Result.result(annunciateMapper.delete(queryWrapper));
    }

    @Override
    public Result stickAnnunciate( Integer id) {
        //置顶 修改Sort 跟时间；
        return Result.result(annunciateMapper.stickAnnunciate(id));
    }


    /**
     * 给分页加上  时间倒序；  sort正序的条件；
     * @param myPage
     * @param queryWrapper
     * @return
     */
    private Result result(MyPage myPage,QueryWrapper queryWrapper,boolean filter) {
        //先排序sort； 默认时间倒序；
        queryWrapper.orderByAsc("sort");
        queryWrapper.orderByDesc("upTime");
        if (filter) {
            queryWrapper.eq("state", 0);
        }
        return   Result.result(annunciateMapper
                .selectPage(new Page<Annunciate>(myPage.getPage(),myPage.getLimit()),queryWrapper));
    }

}
